package com.ruoyi.biz.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.biz.domain.BizCatenarySwitchRecord;
import com.ruoyi.biz.domain.BizSingleLineAnnotation;
import com.ruoyi.biz.mapper.BizSingleLineAnnotationMapper;
import com.ruoyi.biz.request.ControlSwitchRequest;
import com.ruoyi.biz.result.CatenarySwitchAndLinesResult;
import com.ruoyi.biz.service.IBizCatenarySwitchRecordService;
import com.ruoyi.biz.service.IBizSingleLineAnnotationService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.mapper.BizCatenarySwitchMapper;
import com.ruoyi.biz.domain.BizCatenarySwitch;
import com.ruoyi.biz.service.IBizCatenarySwitchService;

/**
 * 接触网开关数据管理Service业务层处理
 *
 * @author cc
 * @date 2025-11-11
 */
@Service
public class BizCatenarySwitchServiceImpl implements IBizCatenarySwitchService {
    @Autowired
    private BizCatenarySwitchMapper bizCatenarySwitchMapper;
    @Autowired
    private IBizSingleLineAnnotationService bizSingleLineAnnotationService;
    @Autowired
    private IBizCatenarySwitchRecordService bizCatenarySwitchRecordService;

    /**
     * 查询接触网开关数据管理
     *
     * @param id 接触网开关数据管理主键
     * @return 接触网开关数据管理
     */
    @Override
    public BizCatenarySwitch selectBizCatenarySwitchById(Long id) {
        return bizCatenarySwitchMapper.selectBizCatenarySwitchById(id);
    }

    /**
     * 查询接触网开关数据管理列表
     *
     * @param bizCatenarySwitch 接触网开关数据管理
     * @return 接触网开关数据管理
     */
    @Override
    public List<BizCatenarySwitch> selectBizCatenarySwitchList(BizCatenarySwitch bizCatenarySwitch) {
        return bizCatenarySwitchMapper.selectBizCatenarySwitchList(bizCatenarySwitch);
    }

    /**
     * 新增接触网开关数据管理
     *
     * @param bizCatenarySwitch 接触网开关数据管理
     * @return 结果
     */
    @Override
    public int insertBizCatenarySwitch(BizCatenarySwitch bizCatenarySwitch) {
        bizCatenarySwitch.setCreateTime(DateUtils.getNowDate());
        return bizCatenarySwitchMapper.insertBizCatenarySwitch(bizCatenarySwitch);
    }

    /**
     * 修改接触网开关数据管理
     *
     * @param bizCatenarySwitch 接触网开关数据管理
     * @return 结果
     */
    @Override
    public int updateBizCatenarySwitch(BizCatenarySwitch bizCatenarySwitch) {
        bizCatenarySwitch.setUpdateTime(DateUtils.getNowDate());
        return bizCatenarySwitchMapper.updateBizCatenarySwitch(bizCatenarySwitch);
    }

    /**
     * 批量删除接触网开关数据管理
     *
     * @param ids 需要删除的接触网开关数据管理主键
     * @return 结果
     */
    @Override
    public int deleteBizCatenarySwitchByIds(Long[] ids) {
        return bizCatenarySwitchMapper.deleteBizCatenarySwitchByIds(ids);
    }

    /**
     * 删除接触网开关数据管理信息
     *
     * @param id 接触网开关数据管理主键
     * @return 结果
     */
    @Override
    public int deleteBizCatenarySwitchById(Long id) {
        return bizCatenarySwitchMapper.deleteBizCatenarySwitchById(id);
    }


    /**
     * 通过图纸id获取接触网开关数据
     *
     * @param drawingId
     * @return
     */
    @Override
    public List<BizCatenarySwitch> getByDrawingId(Long drawingId) {
        BizCatenarySwitch queryParam = new BizCatenarySwitch();
        queryParam.setDrawingId(drawingId);
        return bizCatenarySwitchMapper.selectBizCatenarySwitchList(queryParam);
    }

    @Override
    public List<CatenarySwitchAndLinesResult> getSwitchAndLinesByDrawingId(Long drawingId) {
        List<CatenarySwitchAndLinesResult> result = new ArrayList<>();

        List<BizCatenarySwitch> switchList = this.getByDrawingId(drawingId);
        List<BizSingleLineAnnotation> linesList = bizSingleLineAnnotationService.getLinesByDrawingId(drawingId);

        Map<Long, List<BizCatenarySwitch>> switchMap = switchList.stream().collect(Collectors.groupingBy(BizCatenarySwitch::getId));
        Map<Long, List<BizSingleLineAnnotation>> lineMap = linesList.stream().collect(Collectors.groupingBy(BizSingleLineAnnotation::getCatenarySwitchId));
        Set<Long> switchIds = new HashSet<>(switchMap.keySet());
        switchIds.addAll(lineMap.keySet());

        switchIds.forEach(id -> {
            result.add(new CatenarySwitchAndLinesResult(switchMap.containsKey(id) ? switchMap.get(id).get(0) : null, lineMap.get(id)));
        });

        return result;
    }

    @Override
    public boolean control(ControlSwitchRequest request, LoginUser loginUser) {

        Date date = new Date();

        BizCatenarySwitch bizCatenarySwitch = this.selectBizCatenarySwitchById(request.getId());
        Date operatorTime = bizCatenarySwitch.getOperatorTime();
        bizCatenarySwitch.setStatus(request.getCmd());
        bizCatenarySwitch.setOperatorTime(date);
        bizCatenarySwitch.setUpdateBy(loginUser.getUsername());
        bizCatenarySwitch.setExt(request.getRemark());
        this.updateBizCatenarySwitch(bizCatenarySwitch);

        BizCatenarySwitchRecord record = new BizCatenarySwitchRecord();
        record.setDrawingId(bizCatenarySwitch.getDrawingId());
        record.setSwitchId(bizCatenarySwitch.getId());
        record.setSwitchName(bizCatenarySwitch.getSwitchName());
        record.setDeptId(bizCatenarySwitch.getDeptId());
        record.setCreateBy(loginUser.getUsername());
        record.setOperatorId(loginUser.getUserId());
        record.setOperatorWorkNumber(loginUser.getUsername());
        record.setOperatorName(loginUser.getUser().getNickName());
        record.setConfirmerId(1L);
        record.setConfirmerName(request.getConfirmerName());
        record.setConfirmerWorkNumber("000101");
        record.setCmd(request.getCmd());
        record.setDescription(request.getRemark());
        if(request.getCmd().equals("CLOSE")){
            record.setCloseTime(date);
            record.setOpenTime(operatorTime);
        }else{
            record.setOpenTime(date);
            record.setCloseTime(operatorTime);
        }

        bizCatenarySwitchRecordService.insertBizCatenarySwitchRecord(record);

        return true;
    }
}
